// pages/awardInfo/index.js
const app = getApp()
import api from "../../config/api"
import request from "../../utils/request"
import { isPhoneRight } from "../../utils/util"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 地址
    detailAddress: '',
    // 联系电话-有空格
    contactPhone: '',
    // 联系电话-无空格
    phone: '',
    // 联系名字
    contact: '',
    showDate: false,
    region:[],
    regionText:'',
    // 点击按钮是否可点击
    canClick: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 获取输入的值
   */
  handleChange(e) {
    const {
      currentTarget = {}, detail = {}
    } = e;
    const {
      dataset: {
        key
      }
    } = currentTarget;

    if(key === 'contactPhone'){
      let phoneVal = detail.value.replace(/\s*/g,"")
      detail.value = phoneVal.replace(/^(\d{3})(\d{4})/g, '$1 $2 ').replace(/\s*$/,'')
    }
    if(key === 'contact'){
      detail.value = detail.value.replace(/\s*/g,"")
    }
    this.setData({
      [key]: detail.value
    });

    this.handleCheck()
  },
  // 检查必填项是否都已填写
  handleCheck(){
    const {
      detailAddress,
      contact,
      contactPhone,
      region
    } = this.data;
    
    if(detailAddress && contact &&  contactPhone && region.length>0){
      this.setData({
        canClick: true
      })
    }else{
      this.setData({
        canClick: false
      })
    }
  },
  // 选择地址的时候输入框失焦
  handleSelectAddress(){
    wx.hideKeyboard()
  },
  /**
   * 选择地址
   */
  bindRegionChange:function(e){
    let val = e.detail.value
    this.setData({
      region: val,
      regionText: val[0] + val[1] + val[2]
    })
    this.handleCheck()
  },
  /**
   * 提交预约单
   */
  handleSave() {
    const {
      detailAddress,
      contact,
      contactPhone,
      region,
      canClick
    } = this.data;
    
    if(!canClick){
      return
    } 
    if (contact.match(/^[ ]*$/)) {
      wx.showToast({
        title: '请输入领奖人姓名',
        icon: 'none'
      })
      return
    }
    let nameReg= /[^A-Za-z\u4e00-\u9fa5]+/;
    if(nameReg.test(contact)){
      wx.showToast({
        title: '姓名只能输入中文或英文',
        icon: 'none'
      })
      return 
    }

    let phone = contactPhone.replace(/\s*/g,"")
    if (phone.match(/^[ ]*$/)) {
      wx.showToast({
        title: '请输入领奖人手机号',
        icon: 'none'
      })
      return
    }

    if(!isPhoneRight(phone)){
      wx.showToast({
        title: '请正确填写手机号',
        icon:'none'
      })
      return
    }
    if(region.length===0){
      wx.showToast({
        title: '地址不能为空',
        icon:'none'
      })
      return
    }
    if (detailAddress.match(/^[ ]*$/)) {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'none'
      })
      return
    }
    this.setData({
      phone: phone
    })
    
    this.submitFormData();
  },

  /**
   * 提交数据
   */
  async submitFormData() {
    if (this.loading) return
    this.loading = true
    const {
      detailAddress,
      contact,
      region,
      phone
    } = this.data;
    const patientInfo = wx.getStorageSync('patientInfo')

    const params = {
      address: detailAddress,
      cityName: region[1],
      districtName: region[2],
      honoreeName: contact,
      honoreePhone: phone,
      patientId: patientInfo.id,
      provinceName: region[0]
    }
    let res = await request.post(api.saveHonoreeInfo, params, {
      loading: true
    })
    if (res.httpStatus === 200) {
      wx.showToast({
        title: '提交成功',
        icon: 'none'
      })
      // 跳转活动页面
      setTimeout(()=>{
        // 返回活动页面
        app.backToPath('activity/practiceAward/index')
      },1500)
    } else {
      this.loading = false
    }
  },
  /**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function(e) {
    
	},
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {},
})